package com.das.mechanic_base.adapter.groundpush;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.das.mechanic_base.R;
import com.das.mechanic_base.app.X3Constent;
import com.das.mechanic_base.bean.main.GiftBalanceBean;
import com.das.mechanic_base.utils.SpHelper;
import com.das.mechanic_base.utils.X3StringUtils;
import com.kproduce.roundcorners.RoundImageView;

import java.util.ArrayList;
import java.util.List;

public class X3SelectGiftRecordAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<GiftBalanceBean.CreditPresentVoListBean> mList;
    /**
     * 空数据时，显示空布局类型
     */
    private final int EMPTY_VIEW = 1;
    /**
     * 控制空布局的显隐
     */
    private int mEmptyType = 0;

    private String moneyUnit;

    public X3SelectGiftRecordAdapter(Context mContext) {
        this.mContext = mContext;
        moneyUnit = (String) SpHelper.getData(X3Constent.MONEYCODE, "£");
        mList = new ArrayList<>();
        mEmptyType = 1;
    }

    public void changeData(List<GiftBalanceBean.CreditPresentVoListBean> littleAssistantBean) {
        this.mList = littleAssistantBean;
        moneyUnit = (String) SpHelper.getData(X3Constent.MONEYCODE, "£");
        if (this.mList != null && this.mList.size() > 0) {
            // 显示正常的布局
            mEmptyType = 0;
        } else {
            mEmptyType = 1;
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(viewType==EMPTY_VIEW){
            return new EmptyHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_empty_post_son_item, parent, false));
        }
        return new AssistantHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_adapter_gift_record, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == 0) {
            AssistantHolder holder1 = (AssistantHolder) holder;
            GiftBalanceBean.CreditPresentVoListBean homeGetIMBean = mList.get(position);
            holder1.visit_name_tv.setText(homeGetIMBean.getOperatorName());

            holder1.visit_car_tv.setText(String.format(mContext.getString(R.string.x3_top_up_money), homeGetIMBean.getPresentMoney() + "",moneyUnit));
            holder1.visit_date_tv.setText(homeGetIMBean.getPeriodDesc());
            //设置头像圆角图片
//            holder1.iv_icon_imgs.setType(UserDefinedCircleImageView.TYPE_ROUND);
//            holder1.iv_icon_imgs.setBorderRadius(4);
            //设置图片
            Glide.with(mContext).load(X3StringUtils.getImageUrl(homeGetIMBean.getImgUrl())).into(holder1.iv_icon_imgs);
        }else {
            EmptyHolder holder1 = (EmptyHolder) holder;
            holder1.tv_empty.setText(mContext.getString(R.string.x3_gift_no_record_jl) + "");
            holder1.itemView.setOnClickListener(null);
        }
    }


    @Override
    public int getItemCount() {
        return (mList != null && mList.size() > 0) ? mList.size() : mEmptyType;
    }

    @Override
    public int getItemViewType(int position) {
        if (mEmptyType == EMPTY_VIEW) {
            //空布局的类型
            return EMPTY_VIEW;
        }
        return super.getItemViewType(position);
    }

    class AssistantHolder extends RecyclerView.ViewHolder {
        private RoundImageView iv_icon_imgs;
        private TextView visit_name_tv;
        private TextView visit_car_tv;
        private TextView visit_date_tv;

        public AssistantHolder(@NonNull View itemView) {
            super(itemView);
            visit_date_tv = itemView.findViewById(R.id.visit_date_tv);
            iv_icon_imgs = itemView.findViewById(R.id.iv_icon_imgs);
            visit_name_tv = itemView.findViewById(R.id.visit_name_tv);
            visit_car_tv = itemView.findViewById(R.id.visit_car_tv);
        }
    }
    class EmptyHolder extends RecyclerView.ViewHolder {
        TextView tv_empty;

        public EmptyHolder(@NonNull View itemView) {
            super(itemView);
            tv_empty = itemView.findViewById(R.id.tv_empty);
        }
    }
}